import java.util.Arrays;
import java.util.Random;

public class Test {

    //初始化数组，有序
    public static void initOrder(int[] array) {
        for (int i = 0; i < array.length; i++) {
            //最好情况 顺序排列
            //array[i] = i;
            //最坏情况 逆序排列
            array[i] = array.length - i;
        }
    }

    //初始化数组,无序
    public static void notOrder(int[] array) {
        Random random = new Random(); //生成随机数
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10000);//随机数的范围是[0~10000)
        }
    }

    //测试插入排序的运行时间
    public static void testInsertSort(int[] array) {
        //直接插入排序改变的是数组本身，因还需测试其它排序
        //所以需要拷贝一份数组，改变拷贝的数组，防止通过形参引用改变实参引用指向对象的内容
        int[] tmpArray = Arrays.copyOf(array, array.length);
        //记录排序前的时间
        long startTime = System.currentTimeMillis();
        Sort.insertionSort(tmpArray);
        //记录排序后的时间
        long endTime = System.currentTimeMillis();
        System.out.println("直接插入排序耗时：" + (endTime - startTime));
    }


    //测试希尔排序的运行时间
    public static void testShellSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.shellSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序耗时：" + (endTime - startTime));
    }

    public static void testSelectSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.selectSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序耗时：" + (endTime - startTime));
    }

    public static void testSelectSort2(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.selectSort2(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("双向选择排序耗时：" + (endTime - startTime));
    }


    public static void testHeapSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.heapSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("堆排序耗时：" + (endTime - startTime));
    }


    public static void testQuickSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.quickSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("快速排序耗时：" + (endTime - startTime));
    }

    public static void testMergeSort(int[] array) {
        int[] tmpArray = Arrays.copyOf(array, array.length);
        long startTime = System.currentTimeMillis();
        Sort.mergeSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("归并排序耗时：" + (endTime - startTime));
    }

    public static void main2(String[] args) {
        //给一个很大的数组，并初始化
        int[] array = new int[10_0000];
        initOrder(array);
        //notOrder(array);
        //testInsertSort(array);
        testShellSort(array);
        //testSelectSort(array);
        //testSelectSort2(array);
        testHeapSort(array);

        testQuickSort(array);

        testMergeSort(array);


    }


    public static void main(String[] args) {
        int[] array = {27, 15, 19, 18, 28, 34, 65, 49, 25, 37};

        /*Sort.countSort(array);
        System.out.println(Arrays.toString(array));*/
        
        /*Sort.mergeSort(array);
        System.out.println(Arrays.toString(array));*/

        /*Sort.mergeSortNor(array);
        System.out.println(Arrays.toString(array));*/

        /*Sort.quickSort(array);
        System.out.println(Arrays.toString(array));*/


        Sort.quickSortNor(array);
        System.out.println(Arrays.toString(array));


        /*Sort.insertionSort(array);
        System.out.println(Arrays.toString(array));*/

        /*Sort.shellSort(array);
        System.out.println(Arrays.toString(array));*/

        /*Sort.selectSort(array);
        System.out.println(Arrays.toString(array));*/

        /*int[] array = {100, 15, 9, 28, 18};
        Sort.SelectSort2(array);
        System.out.println(Arrays.toString(array));*/

        //int[] array = {100, 15, 9, 28, 18};
        /*Sort.heapSort(array);
        System.out.println(Arrays.toString(array));*/
    }
}
